package com.leoren.run.utils.trace;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.model.LatLng;
import com.leoren.run.log.L;
import com.leoren.run.vo.PathRecord;
import com.leoren.run.vo.RecordPath;
import com.leoren.run.vo.RoutePath;
import com.leoren.run.vo.RunMapRouteVo;
import com.leoren.run.vo.RunRouteVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 路径工具类
 */
public class PathUtils {


    public static  PathRecord assemblePathRecord(RunMapRouteVo mapVo){
        PathRecord record = new PathRecord();
        List<AMapLocation> pathLinePoints = getPathLinePoints(mapVo.getRunRouteVos());
        String distance = mapVo.getDistince() + "";
        String duration = mapVo.getTime() + "";
        String speed = (mapVo.getDistince() / mapVo.getTime()) + "";
        AMapLocation firstLocation = pathLinePoints.get(0);
        AMapLocation endLocation = pathLinePoints.get(pathLinePoints.size() - 1);
        record.setmStartPoint(firstLocation);
        record.setmEndPoint(endLocation);
        record.setmPathLinePoints(pathLinePoints);
        record.setmDistance(distance);
        record.setmDuration(duration);
        record.setmAveragespeed(speed);
        record.setmDate("==");
        return record;
    }

    private static List<AMapLocation> getPathLinePoints(List<RunRouteVo> runRouteVos){
        List<AMapLocation> locationList = new ArrayList<>();
        for(int i = 0; i < runRouteVos.size(); i++){
            RunRouteVo runRouteVo = runRouteVos.get(i);
            List<RoutePath> routePaths = runRouteVo.getRoutePaths();
            for(int j = 0; j < routePaths.size(); i++){
                RoutePath routePath = routePaths.get(j);
                AMapLocation location = getAmapLocation(routePath);
                locationList.add(location);
            }
        }
        return locationList;
    }

    private static AMapLocation getAmapLocation(RoutePath routePath){
        if(routePath == null){
            return null;
        }
        AMapLocation location = new AMapLocation("gps");
        location.setLatitude(floatToDouble(routePath.getP()));
        location.setLongitude(floatToDouble(routePath.getQ()));
        return location;
    }

    private static double floatToDouble(float f){
        BigDecimal b = new BigDecimal(String.valueOf(f));
        double d = b.doubleValue();
        return d;
    }


    public static List<LatLng> parseLatLngList(List<AMapLocation> list) {
        List<LatLng> traceList = new ArrayList<LatLng>();
        if (list == null) {
            return traceList;
        }
        for (int i = 0; i < list.size(); i++) {
            AMapLocation loc = list.get(i);
            double lat = loc.getLatitude();
            double lng = loc.getLongitude();
            LatLng latlng = new LatLng(lat, lng);
            traceList.add(latlng);
        }
        return traceList;
    }

    /**
     * 根据mapVo得到位置列表
     * @param mapVo
     * @return
     */
    public static List<LatLng> GetPathList(RunMapRouteVo mapVo) {
        L.e("mapVo = ==== " + mapVo);
        List<LatLng> pathPoints = getPathPoints(mapVo.getRunRouteVos());
        L.e("Utils = " + pathPoints);
        return pathPoints;
    }

    /**
     * RunMapRouteVo得到位置列表
     * @param mapVo
     * @param userId
     * @return
     */
    public static List<RecordPath> GetPathList1(RunMapRouteVo mapVo, Long userId) {
        L.e("mapVo = ==== " + mapVo);
        List<RecordPath> pathPoints = new ArrayList<>();
        List<RunRouteVo> runRouteVos = mapVo.getRunRouteVos();
        int count = 0;
        for(int i = 0; i < runRouteVos.size(); i++){
            RunRouteVo routeVo = runRouteVos.get(i);
            List<RoutePath> routePaths = routeVo.getRoutePaths();
            for(int j = 0; j < routePaths.size(); j++){
                RoutePath routePath = routePaths.get(j);
                pathPoints.add(RecordPath.AssembleRecordPath(routePath, count, userId));
                count++;
            }
        }

        return pathPoints;
    }

    private static List<LatLng> getPathPoints(List<RunRouteVo> routeVos){
        List<LatLng> latLngs = new ArrayList<>();
        for(int i = 0; i < routeVos.size(); i++){
            RunRouteVo routeVo = routeVos.get(i);
            List<RoutePath> routePaths = routeVo.getRoutePaths();
            for(int j = 0; j < routePaths.size(); j++){
                RoutePath routePath = routePaths.get(j);
                LatLng location = getLocation(routePath);
                latLngs.add(location);
            }
        }
        return latLngs;
    }

    private static LatLng getLocation(RoutePath path){
        return new LatLng(path.getP(), path.getQ());
    }
}
